package com.kifas.service.dataaccess;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.NoResultException;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.TransactionStokEntity;

/**
 * Created by IntelliJ IDEA.
 * User: brainmaster
 * Date: 4/28/11
 * Time: 12:32 AM
 */
@Repository
public class TransactionStokDao {

    @Autowired
    private SessionFactory sessionFactory;

    private static final String ALL_TRANSACTION = " from TransactionStokEntity ts ";// +
//			"order by ts.idBarang, ts.idSatuan, ts.idGudang, ts.bulanKadaluarsa";
    private static final String ALL_TRANSACTION_ORDER = ALL_TRANSACTION + 
    		"order by ts.idBarang, ts.idGudang, ts.bulanKadaluarsa";
    private static final String TRANSACTION_BY_REFERENCE = ALL_TRANSACTION + " where ts.reference = :reference " +
			"order by ts.idBarang,  ts.idGudang, ts.bulanKadaluarsa";
    private static final String TRANSACTION_BY_DATE = ALL_TRANSACTION + " where ts.tanggal between :yesterday and :today " +
    		"order by ts.idBarang, ts.idGudang, ts.bulanKadaluarsa";
    private static final String TRANSACTION_BY_CURRENT_MONTH = ALL_TRANSACTION + " where month(ts.tanggal) = month(current_date()) " +
    		"and day(ts.tanggal) < day(current_date()) " +
    		"order by ts.idBarang, ts.idGudang, ts.bulanKadaluarsa";

    private static final String TRANSACTION_BY_CURRENT_MONTH_TGL_1 = ALL_TRANSACTION + " where month(ts.tanggal) = month(current_date()) - 1 " +
    		"order by ts.idBarang, ts.idGudang, ts.bulanKadaluarsa";

    public void insert(TransactionStokEntity transactionStokEntity) throws Exception {
        sessionFactory.getCurrentSession().save(transactionStokEntity);
    }

    @SuppressWarnings("unchecked")
    public List<TransactionStokEntity> getTransactionStoks() {
        return sessionFactory.getCurrentSession()
                .createQuery(ALL_TRANSACTION_ORDER)
                .list();
    }
    
    @SuppressWarnings("unchecked")
    public List<TransactionStokEntity> getCurrentMonthTransactionStoks() {
//        Calendar cal = new GregorianCalendar();
//        cal.setTime(new Date());
//        String query = "";
//        if (cal.get(Calendar.DAY_OF_MONTH) == 1){
//            query = TRANSACTION_BY_CURRENT_MONTH_TGL_1;
//        }else{
//            query = TRANSACTION_BY_CURRENT_MONTH;
//        }

        List<TransactionStokEntity> list = sessionFactory.getCurrentSession()
                .createQuery(TRANSACTION_BY_CURRENT_MONTH)
                .list();

        if (list.isEmpty()){
            list = sessionFactory.getCurrentSession()
                .createQuery(TRANSACTION_BY_CURRENT_MONTH_TGL_1)
                .list();
        }

        return list;
    }

    @SuppressWarnings("unchecked")
    public List<TransactionStokEntity> getTodayTransactionStoks() {
        GregorianCalendar calendar = new GregorianCalendar();
        Date today = calendar.getTime();
        calendar.add(Calendar.DATE, -1);
        Date yesterday = calendar.getTime();
        return sessionFactory.getCurrentSession()
                .createQuery(TRANSACTION_BY_DATE)
                .setDate("yesterday", yesterday)
                .setDate("today", today)
                .list();
    }
    
    public boolean isTransactionStokByReferenceExist(String reference) {
    	try {
	    	TransactionStokEntity transactionStok = (TransactionStokEntity) sessionFactory.getCurrentSession()
					.createQuery(TRANSACTION_BY_REFERENCE)
					.setString("reference", reference)
					.uniqueResult();
	    	if (transactionStok == null) {
	    		return false;
	    	}
    	} catch (NoResultException e) {
    		return false;
    	}
    	return true;
    }

}
